package com.kehutong.department.controller;

import com.kehutong.common.util.Token;
import com.kehutong.department.entity.ThirdConfigure;
import com.kehutong.department.entity.ThirdConnectionClassification;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;
/**
 * 第三方外部连接分类
 * @author liulei
 */
@MvcAction
@ReqMapping("/admin/third/Classification")
public class ThirdClassificationController {
    @Inject
    private JdbcSession jdbcSession;
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(ThirdConnectionClassification thirdConnectionClassification) {

        // 增加校验不允许有相同的分类名称
        ThirdConnectionClassification item = jdbcSession.findOne(ThirdConnectionClassification.class)
                .eq("classificationName", thirdConnectionClassification.getClassificationName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前分类名称，请修改后重试");
        }
        //插入分类
        jdbcSession.insert(thirdConnectionClassification);
        return Result.success();
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(ThirdConnectionClassification thirdConnectionClassification) throws Exception {
        ThirdConnectionClassification item = jdbcSession.findOne(ThirdConnectionClassification.class)
                .eq("classificationName", thirdConnectionClassification.getClassificationName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前分类名称，请修改后重试");
        }
        jdbcSession.updateById(thirdConnectionClassification);
        return Result.success();
    }

    @ReqMapping("/delete")
    public Object delete(ThirdConnectionClassification thirdConnectionClassification) {
        thirdConnectionClassification.setDeleted(true);
        jdbcSession.updateById(thirdConnectionClassification);
        return Result.success();
    }


    @ReqMapping("/list")
    public Object list(Token token, JSONObject jsonObject) {
        FindPage<ThirdConnectionClassification> find = jdbcSession.findPage(ThirdConnectionClassification.class)
                .like(jsonObject, "classificationName", "classificationName")
                .eq("deleted", false)
                .order("createTime", "DESC")
                .page(jsonObject);
        Page<ThirdConnectionClassification> page=find.exe();
        return page;
    }


    @ReqMapping("/listAll")
    public Object listAll(JSONObject jsonObject) {
        return jdbcSession.findArray(ThirdConnectionClassification.class)
                .eq("deleted", false)
                .order("createTime", "DESC")
                .exe();
    }
}
